package com.beilang.hotline.telrecord.manager.impl;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.*;
import com.beilang.hotline.company.manager.CompanyMgr;
import com.beilang.hotline.company.model.CompanyModel;
import com.beilang.hotline.issue.manager.IssueMgr;
import com.beilang.hotline.issue.model.IssueModel;
import com.beilang.hotline.module.manager.ModuleMgr;
import com.beilang.hotline.module.model.ModuleModel;
import com.beilang.hotline.sys.manager.SysMgr;
import com.beilang.hotline.sys.model.SysModel;
import com.beilang.hotline.telephone.dao.TelephoneDAO;
import com.beilang.hotline.telephone.model.TelephoneModel;
import com.beilang.hotline.telrecord.dao.TelrecordDAO;
import com.beilang.hotline.telrecord.manager.TelrecordMgr;
import com.beilang.hotline.telrecord.model.TelrecordModel;
import com.beilang.hotline.telrecord.to.SearchTO;
import com.beilang.hotline.telreply.manager.TelreplyMgr;
import com.beilang.hotline.title.manager.TitleMgr;
import com.beilang.hotline.title.model.TitleModel;
import com.beilang.hotline.user.manager.UserMgr;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.person.model.PersonModel;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import jxl.Workbook;
import jxl.write.WriteException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class TelrecordMgrImpl implements TelrecordMgr {


    public static final float WIDTH = 1500;
    public static final float HEIGHT = 840;
    public static final String SHEETNAME = "电话列表";
    public static final String USER_SHEETNAME = "用户统计";
    public static final String TEL_SHEETNAME = "电话统计";
    public static final String TELHIST_SHEETNAME = "电话记录";


    private TelrecordDAO telrecordDAO;
    private TelreplyMgr telreplyMgr;
    private CodeLookupMgr codeLookupMgr;
    private CompanyMgr companyMgr;
    private SysMgr sysMgr;
    private ModuleMgr moduleMgr;
    private IssueMgr issueMgr;
    private UserMgr userMgr;
    private TitleMgr titleMgr;
    private TelephoneDAO telephoneDAO;

    public void setTelephoneDAO(TelephoneDAO telephoneDAO) {
        this.telephoneDAO = telephoneDAO;
    }

    public void setTitleMgr(TitleMgr titleMgr) {
        this.titleMgr = titleMgr;
    }

    public void setUserMgr(UserMgr userMgr) {
        this.userMgr = userMgr;
    }

    public void setSysMgr(SysMgr sysMgr) {
        this.sysMgr = sysMgr;
    }

    public void setModuleMgr(ModuleMgr moduleMgr) {
        this.moduleMgr = moduleMgr;
    }

    public void setIssueMgr(IssueMgr issueMgr) {
        this.issueMgr = issueMgr;
    }

    public void setCompanyMgr(CompanyMgr companyMgr) {
        this.companyMgr = companyMgr;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setTelreplyMgr(TelreplyMgr telreplyMgr) {
        this.telreplyMgr = telreplyMgr;
    }

    public void setTelrecordDAO(TelrecordDAO telrecordDAO) {
        this.telrecordDAO = telrecordDAO;
    }

    public TelrecordModel createTelrecord(TelrecordModel telrecordModel) {
        telrecordModel = telrecordDAO.createTelrecordModel(telrecordModel);
        if (telrecordModel != null) {
            telrecordModel = setTelrecordModelDesc(telrecordModel);
        }
        return telrecordModel;
    }

    public TelrecordModel saveTelrecord(TelrecordModel telrecordModel) {
        telrecordModel = telrecordDAO.saveTelrecordModel(telrecordModel);
        if (telrecordModel != null) {
            telrecordModel = setTelrecordModelDesc(telrecordModel);
        }
        return telrecordModel;
    }

    public void deleteTelrecord(Long recordId) {
        telrecordDAO.deleteTelrecordModel(recordId);
    }

    public TelrecordModel getTelrecord(Long recordId) {
        TelrecordModel telrecordModel = telrecordDAO.getTelrecordModel(recordId);
        if (telrecordModel != null) {
            telrecordModel = setTelrecordModelDesc(telrecordModel);
        }
        return telrecordModel;
    }

    public PaginationHelper getTelrecordList(UserModel user, PaginationHelper helper, SearchTO searchTO) {
        telrecordDAO.deleteDraftTelrecord(user);

        int count = telrecordDAO.searchCount(user, searchTO);
        helper.setTotalSize(count);
        List list = telrecordDAO.searchList(user, helper, searchTO);
        helper.setList(list);
        if (list != null && list.size() > 0) {
            List l = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                TelrecordModel telrecordModel = (TelrecordModel) list.get(i);
                telrecordModel = setTelrecordModelDesc(telrecordModel);
                l.add(telrecordModel);
            }
            helper.setList(l);
        } else {
            helper.setList(list);
        }
        return helper;
    }

    public String genereateReport(UserModel user, SearchTO searchTO, String extFormat, String extDetail, String extMode) {

        String fileName = "";

        List list = telrecordDAO.searchReportList(user, searchTO);

        if (Constant.DETAIL.equals(extMode)) {
            if (Constant.PDF.equals(extFormat.toUpperCase())) {
                if (Constant.LIST.equals(extDetail)) {
                    fileName = writeListPDFTableZip(searchTO, list);
                } else if (Constant.DETAIL.equals(extDetail)) {
                    fileName = writeDetailPDFTableZip(searchTO, list);
                }
            } else if (Constant.EXCEL.equals(extFormat.toUpperCase())) {
                if (Constant.LIST.equals(extDetail)) {
                    fileName = writeListExcelTableZip(searchTO, list);
                } else if (Constant.DETAIL.equals(extDetail)) {
                    fileName = writeDetailExcelTableZip(searchTO, list);
                }
            }
        } else if (Constant.LIST.equals(extMode)) {
            if (Constant.PDF.equals(extFormat.toUpperCase())) {
                if (Constant.LIST.equals(extDetail)) {
                    fileName = writeListPDFTableNormal(searchTO, list);
                } else if (Constant.DETAIL.equals(extDetail)) {
                    fileName = writeDetailPDFTableNormal(searchTO, list);
                }
            } else if (Constant.EXCEL.equals(extFormat.toUpperCase())) {
                if (Constant.LIST.equals(extDetail)) {
                    fileName = writeListExcelTableNormal(searchTO, list);
                } else if (Constant.DETAIL.equals(extDetail)) {
                    fileName = writeDetailExcelTableNormal(searchTO, list);
                }
            }
        }
        return fileName;
    }

    public String writeListPDFTableNormal(SearchTO searchTO, List list) {
        ReportUtil reportUtil = new ReportUtil();
        ReportPDFUtil reportPDFUtil = new ReportPDFUtil();
        String fileName = reportUtil.getTempFileName(".pdf");
        try {
            int i = 1;

            Rectangle pageSize = new Rectangle(WIDTH, HEIGHT);
            Document document = new Document(pageSize);

            PdfWriter.getInstance(document, new FileOutputStream(Constant.FILEPATH + fileName));

            document.open();
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int size = reportUtil.getColumnSize(searchTO);
                reportPDFUtil.writePDFVReportTable(size, 0, searchTO, null, null, document);//write header
                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    reportPDFUtil.writePDFVReportTable(size, i, searchTO, telrecordModel, replyList, document);
                    i++;
                }

            } else {
                document.add(new Paragraph(" "));

            }

            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return fileName;
    }

    public String writeDetailPDFTableNormal(SearchTO searchTO, List list) {
        ReportPDFUtil reportPDFUtil = new ReportPDFUtil();
        ReportUtil reportUtil = new ReportUtil();
        String fileName = reportUtil.getTempFileName(".pdf");
        try {

            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, new FileOutputStream(Constant.FILEPATH + fileName));

            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 10, com.lowagie.text.Font.NORMAL);
            document.open();
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    reportPDFUtil.writePDFHReportTable(telrecordModel, replyList, searchTO, document, FontChinese);
                }
            } else {
                document.add(new Paragraph(" "));
            }
            document.close();


        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return fileName;
    }


    public String writeDetailExcelTableNormal(SearchTO searchTO, List list) {
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
        ReportUtil reportUtil = new ReportUtil();
        String fileName = reportUtil.getTempFileName(".xls");
        File file = new File(Constant.FILEPATH + fileName);

        try {
            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
            jxl.write.WritableSheet ws = wwb.createSheet(SHEETNAME, 0);

            reportExcelUtil.formatSheet(ws);
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int row = 0;
                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    row = reportExcelUtil.writeExcelHReportTable(searchTO, row, telrecordModel, replyList, ws) + 5;
                }
            }
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return fileName;

    }

    public String writeListExcelTableNormal(SearchTO searchTO, List list) {
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
        ReportUtil reportUtil = new ReportUtil();
        String fileName = reportUtil.getTempFileName(".xls");
        File file = new File(Constant.FILEPATH + fileName);

        try {
            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
            jxl.write.WritableSheet ws = wwb.createSheet(SHEETNAME, 0);
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int row = 0;

                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    row = reportExcelUtil.writeExcelVReportTable(searchTO, row, telrecordModel, replyList, ws);
                }
                reportExcelUtil.formatSheet1(ws);
            }
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return fileName;
    }


    public String writeListPDFTableZip(SearchTO searchTO, List list) {
        ReportUtil reportUtil = new ReportUtil();
        ReportPDFUtil reportPDFUtil = new ReportPDFUtil();
        String zipfilePath = reportUtil.getTempFileName("");
        File file = new File(Constant.FILEPATH + zipfilePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            int i = 1;
            Rectangle pageSize = new Rectangle(WIDTH, HEIGHT);


            if (list != null && !list.isEmpty()) {

                Iterator itor = list.iterator();
                int size = reportUtil.getColumnSize(searchTO);

                long previousCompanyId = -1;
                long currentCompanyId = -1;
                Document document = null;
                while (itor.hasNext()) {

                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();

                    currentCompanyId = telrecordModel.getCompanyId();

                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    if (currentCompanyId != previousCompanyId) {
                        if (document != null) {
                            i = 1;
                            if (document.isOpen()) {
                                document.close();
                            }
                            document = null;
                        }
                        document = new Document(pageSize);
                        String fileName = telrecordModel.getCompanyNameDesc() + ".pdf";
                        PdfWriter.getInstance(document, new FileOutputStream(Constant.FILEPATH + zipfilePath + "\\" + fileName));
                        document.open();
                    }
                    reportPDFUtil.writePDFVReportTable(size, 0, searchTO, null, null, document);//write header
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    reportPDFUtil.writePDFVReportTable(size, i, searchTO, telrecordModel, replyList, document);
                    i++;

                    previousCompanyId = currentCompanyId;
                }
                if (document.isOpen()) {
                    document.close();
                }
            }

        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return ZipUtil.zipFile(file);
    }

    public String writeDetailPDFTableZip(SearchTO searchTO, List list) {
        ReportPDFUtil reportPDFUtil = new ReportPDFUtil();
        ReportUtil reportUtil = new ReportUtil();
        String zipfilePath = reportUtil.getTempFileName("");
        File file = new File(Constant.FILEPATH + zipfilePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {


            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 10, com.lowagie.text.Font.NORMAL);

            if (list != null && !list.isEmpty()) {
                Document document = null;
                long previousCompanyId = -1;
                long currentCompanyId = -1;
                Iterator itor = list.iterator();
                while (itor.hasNext()) {

                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    currentCompanyId = telrecordModel.getCompanyId();
                    if (currentCompanyId != previousCompanyId) {
                        if (document != null) {
                            if (document.isOpen()) {
                                document.close();
                            }
                            document = null;
                        }
                        document = new Document(PageSize.A4);
                        String fileName = telrecordModel.getCompanyNameDesc() + ".pdf";
                        PdfWriter.getInstance(document, new FileOutputStream(Constant.FILEPATH + zipfilePath + "\\" + fileName));
                        document.open();
                    }

                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    reportPDFUtil.writePDFHReportTable(telrecordModel, replyList, searchTO, document, FontChinese);
                    previousCompanyId = currentCompanyId;
                }
                if (document.isOpen()) {
                    document.close();
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return ZipUtil.zipFile(file);
    }

    public String writeDetailExcelTableZip(SearchTO searchTO, List list) {
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
        ReportUtil reportUtil = new ReportUtil();
        String zipfilePath = reportUtil.getTempFileName("");
        File file = new File(Constant.FILEPATH + zipfilePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            if (list != null && !list.isEmpty()) {
                jxl.write.WritableWorkbook wwb = null;
                jxl.write.WritableSheet ws = null;
                long previousCompanyId = -1;
                long currentCompanyId = -1;
                Iterator itor = list.iterator();
                int row = 0;
                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    currentCompanyId = telrecordModel.getCompanyId();
                    if (currentCompanyId != previousCompanyId) {
                        if (wwb != null) {
                            wwb.write();
                            wwb.close();
                        }
                        row = 0;
                        String fileName = telrecordModel.getCompanyNameDesc() + ".xls";
                        wwb = Workbook.createWorkbook(new File(Constant.FILEPATH + zipfilePath + "\\" + fileName));
                        ws = wwb.createSheet(SHEETNAME, 0);
                        reportExcelUtil.formatSheet(ws);
                    }
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    row = reportExcelUtil.writeExcelHReportTable(searchTO, row, telrecordModel, replyList, ws) + 5;
                    previousCompanyId = currentCompanyId;
                }
                if (wwb != null) {
                    wwb.write();
                    wwb.close();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return ZipUtil.zipFile(file);

    }

    public String writeListExcelTableZip(SearchTO searchTO, List list) {
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
        ReportUtil reportUtil = new ReportUtil();
        String zipfilePath = reportUtil.getTempFileName("");
        File file = new File(Constant.FILEPATH + zipfilePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int row = 0;
                jxl.write.WritableWorkbook wwb = null;
                jxl.write.WritableSheet ws = null;
                long previousCompanyId = -1;
                long currentCompanyId = -1;
                while (itor.hasNext()) {
                    TelrecordModel telrecordModel = (TelrecordModel) itor.next();
                    telrecordModel = setTelrecordModelDesc(telrecordModel);
                    currentCompanyId = telrecordModel.getCompanyId();
                    if (currentCompanyId != previousCompanyId) {
                        if (wwb != null) {
                            wwb.write();
                            wwb.close();
                        }
                        String fileName = telrecordModel.getCompanyNameDesc() + ".xls";
                        wwb = Workbook.createWorkbook(new File(Constant.FILEPATH + zipfilePath + "\\" + fileName));
                        ws = wwb.createSheet(SHEETNAME, 0);
                        row = 0;
                    }
                    List replyList = telreplyMgr.searchTelreplyList(telrecordModel.getRecordId(), false);
                    reportExcelUtil.formatSheet1(ws);
                    row = reportExcelUtil.writeExcelVReportTable(searchTO, row, telrecordModel, replyList, ws);
                }
                if (wwb != null) {
                    wwb.write();
                    wwb.close();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return ZipUtil.zipFile(file);
    }

    public TelrecordModel setTelrecordModelDesc(TelrecordModel telrecordModel) {
        if (telrecordModel == null) {
            return null;
        }

        if (!StringUtil.isEmpty(telrecordModel.getPname())) {
            telrecordModel.setPname(StringUtil.splitString(telrecordModel.getPname()));
        }


        if (telrecordModel.getTitleId() != null && telrecordModel.getTitleId() != 0) {
            TitleModel titleModel = titleMgr.getTitle(telrecordModel.getTitleId());
            if (titleModel != null) {
                telrecordModel.setTitleDesc(titleModel.getTitle());
            }
        }


        if (telrecordModel.getCompanyId() != null && telrecordModel.getCompanyId() != 0) {
            CompanyModel companyModel = companyMgr.getCompany(telrecordModel.getCompanyId());
            if (companyModel != null) {
                telrecordModel.setCompanyNameDesc(StringUtil.splitString(companyModel.getCompanyName()));
            }
        }

        if (telrecordModel.getSystemId() != null && telrecordModel.getSystemId() != 0) {
            SysModel sysModel = sysMgr.getSys(telrecordModel.getSystemId());
            if (sysModel != null) {
                telrecordModel.setSysNameDesc(sysModel.getSysName());
            }
        }

        if (telrecordModel.getModuleId() != null && telrecordModel.getModuleId() != 0) {
            ModuleModel moduleModel = moduleMgr.getModule(telrecordModel.getModuleId());
            if (moduleModel != null) {
                telrecordModel.setModuleNameDesc(moduleModel.getModuleName());
            }
        }

        if (telrecordModel.getIssueId() != null && telrecordModel.getIssueId() != 0) {
            IssueModel issueModel = issueMgr.getIssue(telrecordModel.getIssueId());
            if (issueModel != null) {
                telrecordModel.setIssueNameDesc(issueModel.getIssueName());
            }
        }

        if (!StringUtil.isEmpty(telrecordModel.getCreateId())) {
            UserModel userModel = userMgr.getUser(telrecordModel.getCreateId());
            if (userModel != null) {
                telrecordModel.setCreatorNameDesc(StringUtil.splitString(userModel.getUserName()));
            }
        }

        if (!StringUtil.isEmpty(telrecordModel.getCurrentOwner())) {
            UserModel userModel = userMgr.getUser(telrecordModel.getCurrentOwner());
            if (userModel != null) {
                telrecordModel.setCurOwnerNameDesc(StringUtil.splitString(userModel.getUserName()));
            }
        }

        if (!StringUtil.isEmpty(telrecordModel.getSex())) {
            telrecordModel.setSexDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SEX_LIST, telrecordModel.getSex()));
        }

        if (!StringUtil.isEmpty(telrecordModel.getCustomArea())) {
            telrecordModel.setCustomAreaDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_CUSTOM_AREA_LIST, telrecordModel.getCustomArea()));
        }

        if (!StringUtil.isEmpty(telrecordModel.getSystemUpdateStatus())) {
            telrecordModel.setSystemUpdateStatusDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SYSUPDATE_STATUS_LIST, telrecordModel.getSystemUpdateStatus()));
        }

        if (!StringUtil.isEmpty(telrecordModel.getRecordStatus())) {
            String desc = codeLookupMgr.getCodeDesc(CodeConstant.CODE_STATUS_LIST, telrecordModel.getRecordStatus());
            if (!StringUtil.isEmpty(telrecordModel.getSystemUpdateStatus())) {
                telrecordModel.setRecordStatusDesc(desc + "(" + telrecordModel.getSystemUpdateStatusDesc() + ")");
            } else {
                telrecordModel.setRecordStatusDesc(desc);
            }
        }

        telrecordModel.setRecordTimeStr(DateUtil.formatDateHHMM(telrecordModel.getRecordTime()));

        return telrecordModel;
    }

    public byte[] genereatevoicefileReport(Date dateFrom, Date dateTo, int workDays) {
        byte[] buf = new byte[0];

        try {
            ReportUtil reportUtil = new ReportUtil();

            String fileName = Constant.FILEPATH + reportUtil.getTempFileName(".xls");
            File xlsfile = new File(fileName);
            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(xlsfile);


            genereateUserNumberRpt(dateFrom, dateTo, workDays, wwb);
            genereateTelHistoryRpt(dateFrom, dateTo, wwb);
            wwb.write();
            wwb.close();
            buf = reportUtil.readByteArrayOfSpecifiedFile(fileName);
            xlsfile.delete();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return buf;
    }

    public void genereateUserNumberRpt(Date dateFrom, Date dateTo, int workDays, jxl.write.WritableWorkbook wwb) {
        jxl.write.WritableSheet ws1 = wwb.createSheet(TEL_SHEETNAME, 0);
        jxl.write.WritableSheet ws = wwb.createSheet(USER_SHEETNAME, 1);
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
        reportExcelUtil.writeUserNumberRptHeader(dateFrom, dateTo, ws, workDays);
        reportExcelUtil.writeUserNumberRptHeader(dateFrom, dateTo, ws1, workDays);
        List list1 = telephoneDAO.searchUserRptList(dateFrom, dateTo);
        List list2 = telephoneDAO.searchNumberRptList(dateFrom, dateTo);

        reportExcelUtil.writeUserNumberRptDetail(ws, list1, "U", workDays);
        reportExcelUtil.writeUserNumberRptDetail(ws1, list2, "T", workDays);
    }

    public void genereateTelHistoryRpt(Date dateFrom, Date dateTo, jxl.write.WritableWorkbook wwb) {
        jxl.write.WritableSheet ws = wwb.createSheet(TELHIST_SHEETNAME, 2);
        List list = telephoneDAO.searchTelephoneList(dateFrom, dateTo);
        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();

        reportExcelUtil.writeTelHistoryRptHeader(ws);
        int i = 1;
        if (list != null && !list.isEmpty()) {
            Iterator itor = list.iterator();
            while (itor.hasNext()) {
                TelephoneModel telephoneModel = (TelephoneModel) itor.next();
                reportExcelUtil.writeTelHistoryRptDetail(ws, telephoneModel, i);
                i++;
            }
        }
        reportExcelUtil.formatTelHistoryRptDetailSheet(ws);
    }

//    public void getUserTelRpt(Date dateFrom, Date dateTo, String type, jxl.write.WritableSheet ws) {
//        ReportExcelUtil reportExcelUtil = new ReportExcelUtil();
//        List list = new ArrayList();
//        if ("U".equals(type)) {
//            list = telrecordDAO.searchvoicefileUserList(dateFrom, dateTo);
//        } else if ("T".equals(type)) {
//            list = telrecordDAO.searchvoicefileTelList(dateFrom, dateTo);
//        }
//        reportExcelUtil.writeUserTelRptHeader(ws, type, dateFrom, dateTo);
//
//        int totalfileTime = 0;
//        int totalfilesize = 0;
//        if (list != null && !list.isEmpty()) {
//            Iterator itor = list.iterator();
//            int row = 3;
//            while (itor.hasNext()) {
//                String paramId = (String) itor.next();
//                String col1 = "";
//                List recordList = new ArrayList();
//                if ("U".equals(type)) {
//                    recordList = telrecordDAO.searchvoicefileUserRptList(dateFrom, dateTo, paramId);
//                    if (!StringUtil.isEmpty(paramId)) {
//                        UserModel userModel = userMgr.getUser(paramId);
//                        if (userModel != null) {
//                            col1 = StringUtil.splitString(userModel.getUserName());
//                        }
//                    }
//                } else if ("T".equals(type)) {
//                    recordList = telrecordDAO.searchvoicefileTelRptList(dateFrom, dateTo, paramId);
//                    col1 = paramId;
//                }
//
//                int fileTime = 0;
//                Iterator itor1 = recordList.iterator();
//                while (itor1.hasNext()) {
//                    TelrecordModel telrecordModel = (TelrecordModel) itor1.next();
//                    String voiceFileName = telrecordModel.getVoiceFileName();
//                    File file = new File(Constant.VOICE_FILEPATH + StringUtil.trim(voiceFileName));
//                    if (file.exists()) {
//                        fileTime = fileTime + getFileTime(file);
//                    }
//                }
//                totalfileTime = totalfileTime + fileTime;
//                String fileTimeDesc = getFileTimeDesc(fileTime);
//                int fileSize = recordList.size();
//                totalfilesize = totalfilesize + fileSize;
//                reportExcelUtil.writeUserTelRptDetial(ws, row, col1, fileTimeDesc, String.valueOf(fileSize));
//                row++;
//            }
//            reportExcelUtil.writeUserTelRptDetial(ws, row, "总计", getFileTimeDesc(totalfileTime), String.valueOf(totalfilesize));
//        }
//    }


    public int getFileTime(File file) {
        float total = 0;
        try {
            AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
            AudioFormat s = aff.getFormat();
            total = file.length() / s.getFrameRate() / s.getFrameSize();

        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return (int) total;
    }

    public List getSearchCompanyList(String companyName, List companyTypeList) {
          return telrecordDAO.getSearchCompanyList(companyName,companyTypeList);
    }

    public PersonModel getCompanyPersonByTelephone(String telephonenumber) {
       return  telrecordDAO.getCompanyPersonByTelephone(telephonenumber);
    }

}
